package com.fzshang.api.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fzshang.commen.bean.Commodity;
import com.fzshang.commen.bean.PlanceAnOrder;
import com.fzshang.commen.bean.TableNumber;
import com.fzshang.commen.bean.UserImage;
import com.fzshang.commen.controller.BaseController;
import com.fzshang.commen.entity.ApiResult;
import com.fzshang.commen.util.data.Date;
import com.fzshang.commen.util.zhifubao.OrderIdUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.IntStream;
import java.util.stream.Stream;

/**
 * @AUTHOR 冯政尚
 * @DATE 2020/2/4 10:44
 * @DAY_NAME_FULL: 星期二
 */
@Controller
@RequestMapping(value = "QRCode")
public class QRCodeController extends BaseController {

    /**
     * @return  跳转二维码
     */
    @RequestMapping(value = "QRCode.html")
    public String QRCode(Model model){
        model.addAttribute("userId",getUser().getId());
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id",getUser().getId());
        model.addAttribute("tableNumber",tableNumberService.list(queryWrapper));
        return "QRCode/QRCode";
    }

    /**
     * @param model
     * @return 结算详情
     */
    @RequestMapping(value = "QRCodeDetails.html")
    public String QRCodeDetails(Model model,String list){
        List<Commodity> ll = new ArrayList<>();
        String[] str = list.split(",");
        for (int i = 0; i < str.length; i++) {
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("id",str[i]);
            ll.add(commodityService.getOne(queryWrapper));
        }
//        for (int i = 0; i < ll.size(); i++) {
//            ll.get(i).setSubscript(ll.get(i));
//        }
        model.addAttribute("commodity",ll);
        model.addAttribute("listValue",list);
        return "QRCode/QRCodeDetails";
    }



    /**
     * @param model
     * @return 详情
     */
    @RequestMapping(value = "details.html")
    public String details(Model model,Long id){
        Commodity commodity = commodityService.getById(id);
        model.addAttribute("details",commodity);
        //属性分类
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("id",commodity.getAttributeClassificationId());
        model.addAttribute("attributeClassification",attributeClassificationService.getOne(queryWrapper).getAttributeClassification());
        //属性
        QueryWrapper queryWrapper1 = new QueryWrapper();
        queryWrapper1.eq("id",commodity.getAttributeId());
        model.addAttribute("attribute",attributeService.getOne(queryWrapper1).getAttribute());
        //类型
        if(commodity.getType()==1){
            if(commodity.getCommoditySpecifications()==1){
                model.addAttribute("type","大杯");
            }else{
                model.addAttribute("type","大份");
            }
        }else if(commodity.getType()==2){
            if(commodity.getCommoditySpecifications()==1){
                model.addAttribute("type","中杯");
            }else{
                model.addAttribute("type","中份");
            }
        }else{
            if(commodity.getCommoditySpecifications()==1){
                model.addAttribute("type","小杯");
            }else{
                model.addAttribute("type","小份");
            }
        }
        return "QRCode/details";
    }


    /**
     * @return  跳转首页
     */
    @RequestMapping(value = "index.html")
    public String index(Model model,Long userId,Long tableNumberId){
        model.addAttribute("userId",userId);
        //菜品分类s
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id",userId);
        model.addAttribute("varietyClassification",varietyClassificationService.list(queryWrapper));
        //所有的商品
        QueryWrapper queryWrapper1 = new QueryWrapper();
        queryWrapper1.eq("user_id",userId);
        model.addAttribute("commodity",commodityService.list(queryWrapper1));
        model.addAttribute("sessionUserId",userId);
        QueryWrapper queryWrapper2 = new QueryWrapper();
        queryWrapper2.eq("user_id",userId);
        List<UserImage> userImages = userImageService.list(queryWrapper2);
        model.addAttribute("userImage",userImages);
        model.addAttribute("tableNumberId",tableNumberId);
        return "QRCode/index";
    }


    /**
     * @param id
     * @param userId
     * @return 根据菜品分类id 和商家id查询商品
     */
    @RequestMapping(value = "selectClassificationId")
    @ResponseBody
    public ApiResult selectClassificationId(Long id,Long userId){
        if(id==1){
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("user_id",userId);
            return ApiResult.ok(commodityService.list(queryWrapper));
        }else{
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("classification",id);
            queryWrapper.eq("user_id",userId);
            return ApiResult.ok(commodityService.list(queryWrapper));
        }

    }


    /**
     * @param list
     * @return 查询数据
     */
    @RequestMapping(value = "selectByList")
    @ResponseBody
    public ApiResult selectByList(String list){
        List<Commodity> ll = new ArrayList();
        String[] str = list.split(",");
        for (int i = 0; i < str.length; i++) {
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("id",str[i]);
            Commodity commodity = commodityService.getOne(queryWrapper);
            ll.add(commodity);
        }
        return ApiResult.ok(ll);
    }


    /**
     * @param listValue
     * @return 添加订单
     */
    @RequestMapping(value = "saveOrder")
    @ResponseBody
    public ApiResult saveOrder(String listValue,Long tableNumberId,Long userId){
        String[] str = listValue.split(",");
        for (int i = 0; i < str.length; i++) {
            //根据菜品id查询菜品详情
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("id",Long.parseLong(str[i]));
            Commodity commodity = commodityService.getOne(queryWrapper);
            System.out.println("asdasdasd:"+commodity);
            PlanceAnOrder order = new PlanceAnOrder();
            //菜品id
            order.setCommodityId(commodity.getId());
            //菜品单价
            order.setCommodityPrice(commodity.getPrice());
            //桌号id
            order.setTableNumberId(tableNumberId);
            //订单号
            order.setOddNumbers(OrderIdUtil.gen("DC",1L));
            //状态
            order.setState(1);
            //创建时间
            order.setCreateTime(System.currentTimeMillis());
            //规格 份量 杯量
            order.setSpecifications(commodity.getCommoditySpecifications());
            //类型 大中小
            order.setTypeSize(commodity.getType());
            order.setRemarks("暂无备注");
            order.setUserId(userId);
            //id
            order.setId(null);
            if(!planceAnOrderService.save(order)){
                return ApiResult.fail("添加失败");
            }
        }
        return ApiResult.ok();
    }


    /**
     * @param userId
     * @return 根据商家id查询动态
     */
    @RequestMapping(value = "selectSweepOrder")
    @ResponseBody
    public ApiResult selectSweepOrder(Long userId){
        List<PlanceAnOrder> orders = planceAnOrderService.selectByUserId(userId);
        for (int i = 0; i < orders.size(); i++) {
            //时间
            orders.get(i).setTime(Date.date(orders.get(i).getCreateTime()));
            //桌号
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("id",orders.get(i).getTableNumberId());
            TableNumber tableNumber = tableNumberService.getOne(queryWrapper);
            orders.get(i).setTableNumber(tableNumber.getName()+"号桌");
            //菜品
            QueryWrapper queryWrapper1 = new QueryWrapper();
            queryWrapper1.eq("id",orders.get(i).getCommodityId());
            Commodity commodity = commodityService.getOne(queryWrapper1);
            orders.get(i).setName(commodity.getCommodityName());
            //份量
             if(orders.get(i).getTypeSize()==1){
                 if(orders.get(i).getSpecifications()==1){
                     orders.get(i).setWeight("大杯");
                 }else{
                     orders.get(i).setWeight("大份");
                 }
             }else if(orders.get(i).getTypeSize()==2){
                 if(orders.get(i).getSpecifications()==1){
                     orders.get(i).setWeight("中杯");
                 }else{
                     orders.get(i).setWeight("中份");
                 }
             }else{
                 if(orders.get(i).getSpecifications()==1){
                     orders.get(i).setWeight("小杯");
                 }else{
                     orders.get(i).setWeight("小份");
                 }
             }
        }
        return ApiResult.ok(orders);
    }




}
